<?php

namespace App\Console\Commands\Fee;

use App\Enums\OpenShop\InvestmentPromotion\SignOrderEnum;
use App\Models\Fee\FeeAnnualOrderModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use Illuminate\Console\Command;

class UpdateFeeOrderPayStatus extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'fee:update-fee-order-pay-status';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '批量更改门店的年费支付情况';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $shopCode = [];

        //通过shopCode反查shopId
        $shopIds = [];
        OpenShopSignOrderModel::query()
            ->where('status', '!=', SignOrderEnum::ORDER_STATUS_NO)
            ->whereIn('shopCode', $shopCode)
            ->with(['mainProcessRel'])
            ->get()
            ->each(function ($item) use (&$shopIds) {
                if (null != $item->mainProcessRel) {
                    $shopIds[] = $item->mainProcessRel->shopId;
                }
            });
        FeeAnnualOrderModel::query()
            ->whereIn('shopId', $shopIds)
            ->update([
                'payStatus' => FeeAnnualOrderModel::PAY_STATUS_OK
            ]);
    }
}
